<template>
  <div>
    <el-row>
      <el-form :model="form" ref="ruleForm">
        <el-col :span="12">
          <el-form-item label="角色名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" placeholder="请输入角色名称！"></el-input>
          </el-form-item>
          <el-form-item label="角色权限" :label-width="formLabelWidth">
            <el-input v-model="form.roleKey" autocomplete="off" placeholder="请输入角色权限！"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="顺序" :label-width="formLabelWidth">
            <el-input-number v-model="form.roleSort" controls-position="right" :min="1" :max="100"></el-input-number>
          </el-form-item>
          <el-form-item label="角色状态" :label-width="formLabelWidth">
            <el-radio v-model="form.status" label="0">正常</el-radio>
            <el-radio v-model="form.status" label="1">禁用</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="菜单权限" :label-width="formLabelWidth">
            <el-tree
                ref="tree"
                :default-checked-keys="selectMenuIds"
                :data="tableData"
                show-checkbox
                :check-strictly="true"
                node-key="menuId"
                accordion
                :props="{children: 'children',label:'menuName'}">
            </el-tree>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input type="textarea" v-model="form.remark" autocomplete="off" placeholder="请输入备注！"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close-dialog',false)">取 消</el-button>
      <el-button type="primary" @click="saveRole">确 定</el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import buildTree from "@/utils/buildTree";

export default {
  name: "RoleEdit",
  props: ['id'],
  data() {
    return {
      dialogFormVisible: false,
      //弹窗
      form: {
        //roleKey: "worker",
        remark: "",
        roleSort: 1,
        delFlag: "0"
      },
      formLabelWidth: '120px',
      tableData: [],
      selectMenuIds: []
    }
  },
  created() {
    //菜单权限
    request("menu").then(data => {
      this.tableData = buildTree(data, 0)
    })
    //数据回显
    if (this.id) {
      request(`role/${this.id}`).then(data => {
        this.form = data
        this.selectMenuIds = []
        data.menuList.forEach(o => {
          this.selectMenuIds.push(o.menuId)
        })
      })
    }
  },
  methods: {
    //角色新建
    saveRole() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let arr1 = this.$refs.tree.getCheckedKeys()
          let arr2 = this.$refs.tree.getHalfCheckedKeys()
          this.form.menuIds = [...arr1,...arr2];//将两个集合打开再合并
          request("role", this.form.id ? "PUT" : "POST", this.form).then(() => {
            this.$message.success("操作成功")
            this.$emit('close-dialog', true)
          }).catch(() => {
            this.$message.error("操作失败")
          })
        } else {
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>